require("babel-register");

const Koa = require("koa");
const route = require("koa-route");
const app = new Koa();
const fs = require("fs");

// x-response-time
app.use(async (ctx, next) => {
    const start = Date.now();
    await next();
    const ms = Date.now() - start;
    ctx.set("X-Response-Time", `${ms}ms`);
});

app.on("error", (err, ctx) => {
    console.error("server error", err, ctx);
});

// main
const main = ctx => {
    ctx.response.type = "html";
    ctx.response.body = fs.createReadStream("./webapp/index.html");
};
const getJs = ctx => {
    ctx.response.type = ".js";
    ctx.response.body = fs.createReadStream(`./webapp${ctx.url}`);
};
const getCss = ctx => {
    ctx.response.type = ".css";
    ctx.response.body = fs.createReadStream(`./webapp${ctx.url}`);
};

app.use(route.get("/", main));
app.use(route.get("/js/**", getJs));
app.use(route.get("/css/**", getCss));

app.listen(80);

console.log("Listening at http://localhost:80");
